import Vue from 'vue'
import LangText from '@/framework/components/lang/LangText'
import ColumnType from '@/framework/constant/ColumnType'
import SkuListEntity from '@/framework/components/sku/SkuListEntity'
import ColumnUtil from '@/framework/components/table/ColumnUtil'

export default class FormUtil {
  static validate(formRef) {
    return new Promise((resolve, reject) => {
      formRef.validate(r => {
        if (!r) {
          resolve(false)
        } else {
          resolve(true)
        }
      })
    })
  }

  static createFormDataWithColumns(columns, lang = null, defaultValueMap = {}) {
    columns = ColumnUtil.parseColumns(columns)
    const result = {}
    columns.forEach(item => {
      if (!item.hiddenInForm) {
        if (item.lang) { // 多语言
          result[item.prop] = LangText.create(item, lang)
        } else if (item.type === ColumnType.SKU_EDITOR) {
          result[item.prop] = new SkuListEntity()
        } else if (item.dataType === 'array') {
          result[item.prop] = []
        } else {
          result[item.prop] = 'defaultValue' in item ? item.defaultValue : (defaultValueMap[item.prop] || '')
        }
      }
    })
    return result
  }

  static reset(formData, resetData = {}, exceptFields = []) {
    for (const key in formData) {
      if (!Object.prototype.hasOwnProperty.call(formData, key) || exceptFields.includes(key)) {
        continue
      }
      const value = formData[key]
      const resetValue = resetData[key]
      if (Array.isArray(value)) {
        if (resetValue) {
          if (!Array.isArray(resetValue)) {
            formData[key] = resetValue.split(',')
          } else {
            formData[key] = resetValue
          }
        } else {
          formData[key] = []
        }
      } else if (value instanceof SkuListEntity) { // 规格
        // const skuListEntity = new SkuListEntity()
        // skuListEntity.reset()
        value.setIsEdit(false)
        value.reset()
        // formData[key] = skuListEntity
      } else if (value instanceof LangText) { // 多语言
        value.reset()
      } else {
        formData[key] = typeof resetValue === 'undefined' ? '' : resetValue
      }
    }
  }

  // 填充数据
  static fill(formData, fillData) {
    Object.keys(formData).forEach(key => {
      const oldValue = formData[key]
      if (oldValue instanceof SkuListEntity) { // 商品规格
        // const skuListEntity = new SkuListEntity()
        oldValue.setIsEdit(true)
        oldValue.setValue(fillData[key])
        // skuListEntity.setValue(fillData[key])
        // formData[key] = skuListEntity
      } else if (oldValue instanceof LangText) { // 多语言
        oldValue.assignLang(fillData)
      } else if (key in fillData) {
        let value = fillData[key]
        if (Array.isArray(oldValue)) {
          if (!Array.isArray(value) && typeof value === 'string') {
            value = value ? value.split(',') : []
          }
        }
        formData[key] = value
      }
    })
    Object.keys(fillData).forEach(key => {
      if (key in formData) {
        return
      }
      Vue.set(formData, key, fillData[key])
    })
    console.log(formData)
  }
}
